package com.example.memo.Adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.memo.R;
import com.example.memo.entity.NoteInfo;

import java.util.List;

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchHolder>{
    private List<NoteInfo> noteList;
    private SearchAdapter.onItemClickListener onItemClickListener;
    private NoteInfo noteInfo;

    public SearchAdapter() {
    }

    public void addNote(List<NoteInfo> noteList){
        this.noteList = noteList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SearchAdapter.SearchHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_item,parent,false);
        SearchAdapter.SearchHolder searchHolder = new SearchAdapter.SearchHolder(view);
        return searchHolder;
    }

    @Override
    public int getItemCount() {
        if(noteList == null){
            return 0;
        }else{
            return noteList.size();
        }
    }

    @Override
    public void onBindViewHolder(@NonNull SearchAdapter.SearchHolder holder, @SuppressLint("RecyclerView") int position) {
        noteInfo = noteList.get(position);

        holder.date.setText(noteInfo.getDate());
        holder.title.setText(noteInfo.getTitle());
        holder.content.setText(noteInfo.getContent());

        holder.itemView.setOnClickListener(view -> {
            if(onItemClickListener != null){
                onItemClickListener.onItemClick(position);
            }
        });
    }

    public void setOnItemClickListener(SearchAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    class SearchHolder extends RecyclerView.ViewHolder{
        TextView date,title,content;

        public SearchHolder(@NonNull View itemView) {
            super(itemView);
            date = itemView.findViewById(R.id.date);
            title = itemView.findViewById(R.id.title);
            content = itemView.findViewById(R.id.content);
        }
    }

    public interface onItemClickListener{
        void onItemClick(int position);
    }
}
